const mongoose = require("mongoose");
const autoIncrementFactory = require("mongoose-sequence");
const autoIncreament = autoIncrementFactory(mongoose);
const workSchema = new mongoose.Schema(
  {
    uuid: { type: String, required: true }, // 必填的uuid字段
    title: { type: String, required: true }, // 必填的title字段
    desc: { type: String, required: true }, // 必填的desc字段
    coverImg: { type: String, required: true }, // 必填的coverImg字段
    content: { type: Object, required: true }, // 必填的content字段
    isTemplate: { type: Boolean, default: false }, // 可选的isTemplate字段，默认为false
    isPublic: { type: Boolean, default: false }, // 可选的isPublic字段，默认为false
    isHot: { type: Boolean, default: false }, // 可选的isHot字段，默认为false
    author: { type: String, required: true }, // 必填的author字段
    copiedCount: { type: Number, default: 0 }, // 必填的copiedCount字段，默认为0
    status: {
      type: Number,
      enum: [0, 1, 2], // 可选的status字段，只能是0、1、2中的一个，默认为0
      default: 0,
    },
    user: { type: mongoose.Schema.Types.ObjectId, ref: "user" }, // 必填的user字段，引用User模型
    //最后一次编辑时间
    lastEditTime: { type: Date },
    channel: {
      /**数组对象 {
        id: 1
        name: 'xxxx'
    }*/
      type: Array,
    },
  },
  {
    timestamps: true, // 自动添加createdAt和updatedAt字段
    toJSON: {
      transform(_doc, ret) {
        delete ret.__v;
      },
    },
  }
);
workSchema.plugin(autoIncreament, { inc_field: "id", id: "works_id_counter" });
module.exports = workSchema;
